<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['gongyuan:producttype:add']">新建产品分类</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" type="index" />
      <el-table-column label="分类名称" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="排序" prop="sort" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button class="gongyuan-update" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="['gongyuan:producttype:edit']">修改</el-button>
          <el-button class="gongyuan-delete" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['gongyuan:producttype:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { getProtypeList, delProtype } from "@/api/product/protype";
export default {
  name: "Protypelist",
  data() {
    return {
      // 遮罩层
      loading: true,
      list: [],
      total: 0,
      // 查询参数
      queryParams: {
        name: "",
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      this.loading = true;
      const res = await getProtypeList(this.queryParams);
      this.list = res.rows;
      this.total = res.total;
      this.loading = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleAdd() {
      // 此处后台管理添加的路由, path对应组件name须一致,不可再修改
      this.$router.push({ name: "Protypeedit" });
    },

    handleEdit(row) {
      this.$router.push({ name: "Protypeedit", query: { id: row.id } });
    },
    handleDelete(row) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delProtype(row.id);
        })
        .then(() => {
          this.queryParams.pageNum = 1
          this.getList(true);
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
  },
};
</script>
